home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PCGUIA 117
/
PC Guia 117.iso
/
Software
/
Utils
/
Software6
/
Product13
/
googlebar-0.9.5.06-fx.xpi
/
chrome
/
googlebar.jar
/
content
/
googlebarHighlight.js
< prev
next >
Wrap
Text File
|
2005-02-21
|
13KB
|
433 lines
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Googlebar for Mozilla.
*
* The Initial Developer of the Original Code is Andy Edmonds.
* Portions created by the Initial Developer are Copyright (C) 2001
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Andy Boughton
* Andrew Houghton
* John Woods
* Bernd Kuemmerlen
* Robert Mulcahy
* Alfred Kayser
* Reflex
* Gary Turnbull
* Raj Bhaskar
* Joachim Thewes
* Henrik Gemal
* Robert Fernandes
* Joe Chellman
* Franki Cheung
* Alban Fonrouge
* Martin Hassman
* Ufuk Kayserilioglu
* Yoni Gilad
* Tim Schmidt
* timeless
* Francis Turner
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
const GB_PREF_TERM_COLORS = "termColors";
const GB_PREF_UPD_COLORS = "updateColors";
const GB_HIGHLIGHT_ID = "googlebar_highlight";
var myGooglebarHighlight = {
/*** Variable Declarations ***/
mDelayHighlight: false,
// DOMRange used during highlighting
mSearchRange: null,
mStartPt: null,
mEndPt: null,
// Is the highlight active ?
mHighlightOn: false,
// What was the term used for the highlighting ?
mHighlightString: "",
// Save selection during highlighting
mSelection: null,
mSaveSelection: null,
// Colors for highlighting
mTermColors: null,
/*** Function Declarations ***/
checkHighlight: function(aCritValue) {
if (this.mDelayHighlight) {
this.mDelayHighlight = false;
}
if (!aCritValue) {
aCritValue = myGooglebarUtil.getElement('googlebarCriteria').value;
}
var myTerms = googlebarFindTerms(aCritValue);
var myHighlightButton = myGooglebarUtil.getElement("highlight-button");
if (myTerms.length > 0) {
if (this.mHighlightOn) {
this.highLightInPage("on");
}
myHighlightButton.setAttribute("disabled", false);
myHighlightButton.setAttribute("activated", false);
}
else {
if (this.mHighlightOn) {
this.highLightInPage("off");
this.mHighlightOn = false;
}
myHighlightButton.setAttribute("disabled", true);
myHighlightButton.setAttribute("activated", true);
}
},
delayHighlight: function() {
if (this.mDelayHighlight) {
return;
}
else {
this.mDelayHighlight = true;
setTimeout("myGooglebarHighlight.checkHighlight()", 1000);
}
},
displayCheck: function() {
if (this.mHighlightOn) {
setTimeout("myGooglebarHighlight.refreshHighlight()", 100);
}
},
getTermColors: function() {
return this.mTermColors;
},
highlight: function(aRange, aNode) {
var startContainer = aRange.startContainer;
var startOffset = aRange.startOffset;
var endOffset = aRange.endOffset;
var docfrag = aRange.extractContents();
var before = startContainer.splitText(startOffset);
var parent = before.parentNode;
aNode.appendChild(docfrag);
parent.insertBefore(aNode, before);
return aNode;
},
highlightDoc: function(aWord, aColor, aWindow) {
if (!aWindow) {
aWindow = window.self;
}
for (var i = 0; aWindow.frames && i < aWindow.frames.length; i++) {
this.highlightDoc(aWord, aColor, aWindow.frames[i]);
}
var doc = aWindow.document;
if (!doc) {
return;
}
// Remove highligthing
if (!aColor) {
this.removeHighlight(aWindow);
return;
}
if (!("body" in doc)) {
return;
}
var body = doc.body;
var count = body.childNodes.length;
this.mSearchRange = doc.createRange();
this.mStartPt = doc.createRange();
this.mEndPt = doc.createRange();
var baseNode = doc.createElement("layer");
baseNode.setAttribute("style", "-moz-user-select: -moz-all; background-color: " + aColor + ";");
baseNode.setAttribute("id", GB_HIGHLIGHT_ID);
this.mSearchRange.setStart(body, 0);
this.mSearchRange.setEnd(body, count);
this.mStartPt.setStart(body, 0);
this.mStartPt.setEnd(body, 0);
this.mEndPt.setStart(body, count);
this.mEndPt.setEnd(body, count);
this.highlightText(aWord, baseNode);
},
highLightInPage: function(aOverride) {
try {
if (GB_PREF_OBJ.getBoolPref(GB_PREF_UPD_COLORS)) {
var tempColor = GB_PREF_OBJ.getCharPref(GB_PREF_TERM_COLORS).split(',');
if (this.mTermColors.length == tempColor.length) {
this.mTermColors = tempColor;
}
else {
myGooglebarUtil.logMessage("Problem retrieving updated colors - wrong number of colors");
}
}
}
catch (e) {
myGooglebarUtil.logMessage("Problem retrieving updated colors - pref error");
}
// Initialize to current document
var gTheDocument = window._content.document;
var critBox = myGooglebarUtil.getElement("googlebarCriteria");
var hiBtn = myGooglebarUtil.getElement("highlight-button");
var term = critBox.value;
this.removeHighlight();
if (this.mHighlightOn) {
this.mHighlightOn = false;
// this.highlightDoc(null, null);
googlebarCheckSearchbuttons();
// Exit if the term had not changed
// => means that the user clicked to remove highlighting
if (this.mHighlightString == term && aOverride != "on") {
hiBtn.setAttribute("activated", "false");
return;
}
}
this.mHighlightOn = true;
this.mHighlightString = term;
hiBtn.setAttribute("activated", "true");
// wanted to use selection to extend to the word
// and compare with the found range, in order to match
// only whole words
/*
this.mSelection = window._content.getSelection();
this.mSaveSelection = new Array();
for (var i = 0; i < this.mSelection.rangeCount; i++) {
this.mSaveSelection.push(this.mSelection.getRangeAt(i));
}
this.mSelection.removeAllRanges();
*/
var myTerms = googlebarFindTerms(term);
for (var i = 0; i < myTerms.length; i++) {
// only get terms at least 2 characters long
if (myTerms[i].length > 1) {
this.highlightDoc(myTerms[i], this.mTermColors[i % 10]);
}
}
/*
// Revert to previous selection
this.mSelection.removeAllRanges();
for (var i = 0; i < this.mSaveSelection.length; i++) {
this.mSelection.addRange(this.mSaveSelection[i]);
}
*/
googlebarCheckSearchbuttons();
},
highlightText: function(aWord, aBaseNode) {
var retRange = null;
while((retRange = GB_FIND_OBJ.Find(aWord, this.mSearchRange, this.mStartPt, this.mEndPt))) {
// check that we have a whole word
//this.mSelection.addRange(range);
// Highlight
var nodeSurround = aBaseNode.cloneNode(true);
var node = this.highlight(retRange, nodeSurround);
this.mStartPt = node.ownerDocument.createRange();
this.mStartPt.setStart(node, node.childNodes.length);
this.mStartPt.setEnd(node, node.childNodes.length);
}
},
initialize: function() {
this.readPrefs();
},
isHighlightOn: function() {
return this.mHighlightOn;
},
readPrefs: function() {
try {
this.mTermColors = GB_PREF_OBJ.getCharPref(GB_PREF_TERM_COLORS).split(',');
}
catch(e) { myGooglebarUtil.logMessage( "\n" + e + "\n" );
myGooglebarUtil.logMessage('Resetting ' + GB_PREF_TERM_COLORS + ' pref');
GB_PREF_OBJ.setCharPref(GB_PREF_TERM_COLORS, "yellow,lightpink,aquamarine,darkgoldenrod,darkseagreen,lightgreen,rosybrown,seagreen,chocolate,violet");
}
this.mTermColors = GB_PREF_OBJ.getCharPref(GB_PREF_TERM_COLORS).split(',');
GB_PREF_OBJ.setBoolPref(GB_PREF_UPD_COLORS, false);
},
refreshHighlight: function() {
var hiBtn = myGooglebarUtil.getElement("highlight-button");
hiBtn.click();
hiBtn.click();
},
removeHighlight: function(aWindow) {
if (!aWindow) {
aWindow = window.self;
}
var i = 0;
for (i = 0; aWindow.frames && i < aWindow.frames.length; i++) {
this.removeHighlight(aWindow.frames[i]);
}
var doc = aWindow.document;
if(!doc) { return; }
var body = doc.body;
if(!body) { return; }
var elem = null;
var child = null;
var docfrag = null;
var next = null;
var parent = null;
while ((elem = doc.getElementById(GB_HIGHLIGHT_ID))) {
child = null;
docfrag = doc.createDocumentFragment();
next = elem.nextSibling;
parent = elem.parentNode;
while((child = elem.firstChild)) {
docfrag.appendChild(child);
}
parent.removeChild(elem);
parent.insertBefore(docfrag, next);
}
var count = body.childNodes.length;
var terms = myGooglebarUtil.getElement("googlebarCriteria").value;
var termsArray = googlebarFindTerms(terms);
for (i = 0; i < termsArray.length; i++) {
var term = termsArray[i];
var searchRange = doc.createRange();
searchRange.setStart(body, 0);
searchRange.setEnd(body, count);
var startPt = doc.createRange();
startPt.setStart(body, 0);
startPt.setEnd(body, 0);
var endPt = doc.createRange();
endPt.setStart(body, count);
endPt.setEnd(body, count);
var retRange = null;
while((retRange = GB_FIND_OBJ.Find(term, searchRange, startPt, endPt))) {
var startContainer = retRange.startContainer;
elem = null;
try {
elem = startContainer.parentNode;
}
catch (e) {}
if (elem) {
child = null;
docfrag = null;
next = null;
parent = null;
if (elem.getAttribute("id") == GB_HIGHLIGHT_ID) {
docfrag = doc.createDocumentFragment();
next = elem.nextSibling;
parent = elem.parentNode;
while((child = elem.firstChild)) {
docfrag.appendChild(child);
}
startPt = doc.createRange();
startPt.setStartAfter(elem);
parent.removeChild(elem);
parent.insertBefore(docfrag, next);
}
else {
try {
while((elem = elem.parentNode)) {
if (elem.getAttribute("id") == GB_HIGHLIGHT_ID) {
docfrag = doc.createDocumentFragment();
next = elem.nextSibling;
parent = elem.parentNode;
while((child = elem.firstChild)) {
docfrag.appendChild(child);
}
docfrag.appendChild(elem.cloneNode(true));
startPt = doc.createRange();
startPt.setStartAfter(elem);
parent.removeChild(elem);
parent.insertBefore(docfrag, next);
break;
}
}
}
catch(e) {}
startPt = doc.createRange();
startPt.setStart(retRange.endContainer, retRange.endOffset);
}
}
else {
startPt = doc.createRange();
startPt.setStart(retRange.endContainer, retRange.endOffset);
}
startPt.collapse(true);
}
}
}
};
myGooglebarHighlight.initialize();